import moment from 'moment';


const utils = {

    setData(key, value) {
        const val = JSON.stringify(value);
        localStorage.setItem('gdb' + key, val);
    },

    getData(key, defVal) {
        const str = localStorage.getItem('gdb' + key);
        return str ? JSON.parse(str) : defVal;
    },

    removeData(key) {
        return localStorage.removeItem('gdb' + key);
    },

    setToken(val) {
        this.setData("token", val);
    },

    getToken() {
        return this.getData("token");
    },

    signOut() {
        this.removeData("token");
        this.removeData('openKeys');
        this.removeData('selectedMenu');
        this.removeData('panes');
    },

    getDate(time) {
        return this.isEmpty(time, val => moment(val).format("YYYY-MM-DD"), "");
    },

    getDateTime(time) {
        return this.isEmpty(time, val => moment(val).format("YYYY-MM-DD HH:mm:ss"), "");
    },

    getHowLongBefore(time) {
        let seconds = this.isEmpty(time, val => (new Date() - new Date(val).getTime()) / 1000, -1);
        if (seconds < 60) {
            return '现在';
        } else if (seconds / 60 < 60) {
            return Math.floor(seconds / 60) + '分';
        } else if (seconds / 60 / 60 < 24) {
            return Math.floor(seconds / 60 / 60) + '时';
        } else if (seconds / 60 / 60 / 24 < 30) {
            return Math.floor(seconds / 60 / 60 / 24) + '日';
        } else if (seconds / 60 / 60 / 24 / 30 < 12) {
            return Math.floor(seconds / 60 / 60 / 24 / 30) + '月';
        } else {
            return Math.floor(seconds / 60 / 60 / 24 / 30 / 12) + '年';
        }
    },

    isEmpty(val, format, defaultVal) {
        if (val === null || val === undefined || val.length === 0) {
            return defaultVal;
        }
        return format(val);

    },

    /**
     * @desc 获取一个uuid
     * @author
     * @date 
     */
    genUUID: () => {
        const s = [];
        const hexDigits = "0123456789abcdef";
        for (let i = 0; i < 32; i++) {
            s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
        }
        s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
        s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
        s[8] = s[13] = s[18] = s[23];

        const uuid = s.join("");
        return uuid;
    },

    // 日期十以下的数字前面补0
    setDateZero(value) {
        let finalValue = '0000' + value.toString();
        return finalValue.substring(finalValue.length - 2, finalValue.length);
    },

    // 格式化数值 保留两位小数
    formatValue(value) {
        return parseFloat(value).toFixed(2);
    },
    // 格式化数值 三位数后加，
    formatNum(value) {
        return `${value}`.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
    },

    // 时间戳转化
    dateFormat(time) {
        let date = new Date(time * 1000);
        let Y = date.getFullYear() + '-';
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
        let D = date.getDate() + ' ';
        let h = date.getHours() + ':';
        let m = date.getMinutes() + ':';
        let s = date.getSeconds();

        return `${Y}${M}${D}${h}${m}${s}`;
    },
    setImageUrl(list) {
        let urlList = [];
        list.forEach((item) => {
            if (item.response) {
                const {state, data} = item.response;
                if (state === 1) {
                    urlList.push(data.file_url);
                }
            } else {
                urlList.push(item.file_path);
            }
        });
        return urlList.join();
    },
    getUrl(url, data) {
        let reqParam = "";
        if (data) {
            for (let key in data) {
                const item = data[key];
                if (!item) {
                    continue;
                }
                reqParam += key + "=" + item + "&";
            }
            reqParam += "token=" + utils.getToken();
        }
        return url + "?" + reqParam;
    },
    /**
     2  * [dateAddDays 从某个日期增加n天后的日期]
     3  * @param  {[string]} dateStr  [日期字符串]
     4  * @param  {[int]} dayCount [增加的天数]
     5  * @return {[string]}[增加n天后的日期字符串]
     6  */
     dateAddDays(dateStr,dayCount) {
            let tempDate=new Date(dateStr.replace(/-/g,"/"));//把日期字符串转换成日期格式
            let resultDate=new Date((tempDate/1000+(86400*dayCount))*1000);//增加n天后的日期
            let resultDateStr=resultDate.getFullYear()+"-"+(resultDate.getMonth()+1)+"-"+(resultDate.getDate());//将日期转化为字符串格式
            return resultDateStr;
     }
};


export default utils;
